//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Reverse on DataGrid list
// Modify By:      Leo Ren (leoren@wicresoft.com)
// Modify Date:    2011-1-28
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Data;
using BEGDC.CC.BL;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.Utility.ExceptionManagement;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
using NHibernate;
using NHibernate.Expression;

namespace BEGDC.CC.BL.AdvancedSearch
{
	/// <summary>
	/// Summary description for UserService.
	/// </summary>
	[Transactional]
	public class AdvancedSearchService:IAdvancedSearch
	{
		private ISessionManager _sessionManager;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="sessionManager"></param>
		public AdvancedSearchService(ISessionManager sessionManager)
		{
			this._sessionManager = sessionManager;
		}

		#region IAdvancedSearch Members
		
		#region Get
		[Transaction(TransactionMode.Requires)]
		public BEGDC.CC.Entity.AdvancedSearch GetAdvancedSearch( int searchID )
		{
			IList list;
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					list = session.Find("From AdvancedSearch  where SearchID="+searchID);
					if(list.Count>0)
					{
						BEGDC.CC.Entity.AdvancedSearch search = list[0] as BEGDC.CC.Entity.AdvancedSearch;
						return search;
					}
					else
					{
						return null;
					}
				}
				catch(GDCException gdcEx)
				{
					CCS.Utility.LogAccess.WriteErrLog( "GetAdvancedSearch", gdcEx.Message );
					throw gdcEx;
				}
				catch(System.Data.SqlClient.SqlException sqlEx)
				{
					CCS.Utility.LogAccess.WriteErrLog( "GetAdvancedSearch", sqlEx.Message );
					throw new GDCException(ErrorCode.DB_Exception, "DB Exception", sqlEx);
				}
				catch(Exception ex)
				{
					CCS.Utility.LogAccess.WriteErrLog( "GetAdvancedSearch", ex.Message );
					throw new GDCException(ErrorCode.UserService_Exception, "UserService Exception", ex);
				}
				finally
				{
					session.Close();
				}	
			}
		}



		#endregion

		#region Query

		[Transaction(TransactionMode.Requires)]
		public DataTable QueryMySearchList(string employeeId)
		{
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					string cmdText = string.Format("SELECT     * FROM         AdvancedSearch where  (employeeno  = '{0}')", employeeId.ToString());
					
					DataTable dtSearch = BEGDC.Utility.NHibernate.NHibernateUtil.Execute(session, cmdText);
					return dtSearch;
				}
				catch(System.Data.SqlClient.SqlException sqlEx)
				{
					CCS.Utility.LogAccess.WriteErrLog( "QueryMySearchList", sqlEx.Message );
					throw new GDCException(ErrorCode.DB_Exception, "DB Exception", sqlEx);
				}
				catch(Exception ex)
				{
					CCS.Utility.LogAccess.WriteErrLog( "QueryMySearchList", ex.Message );
					throw new GDCException(ErrorCode.QueryService_Exception, "QueryService Exception", ex);
				}
				finally
				{
					session.Close();
				}
			}
		}

		public DataTable QueryMySearchList(string employeeId, string searchType)
		{
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					string cmdText = string.Format("SELECT     * FROM         AdvancedSearch where  (employeeno  = '{0}' and searchtype='{1}')", employeeId.ToString(), searchType);
					
					DataTable dtSearch = BEGDC.Utility.NHibernate.NHibernateUtil.Execute(session, cmdText);
					return dtSearch;
				}
				catch(System.Data.SqlClient.SqlException sqlEx)
				{
					CCS.Utility.LogAccess.WriteErrLog( "QueryMySearchList", sqlEx.Message );
					throw new GDCException(ErrorCode.DB_Exception, "DB Exception", sqlEx);
				}
				catch(Exception ex)
				{
					CCS.Utility.LogAccess.WriteErrLog( "QueryMySearchList", ex.Message );
					throw new GDCException(ErrorCode.QueryService_Exception, "QueryService Exception", ex);
				}
				finally
				{
					session.Close();
				}
			}
		}

		[Transaction(TransactionMode.Requires)]
		public DataTable QueryList(string searchSQL)
		{
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
                    //searchSQL += " order by requireddatefrom desc";
					DataTable dtSearch = BEGDC.Utility.NHibernate.NHibernateUtil.Execute(session, searchSQL);
					return dtSearch;
				}
				catch(System.Data.SqlClient.SqlException sqlEx)
				{
					CCS.Utility.LogAccess.WriteErrLog( "QueryList", sqlEx.Message );
					throw new GDCException(ErrorCode.DB_Exception, "DB Exception", sqlEx);
				}
				catch(Exception ex)
				{
					CCS.Utility.LogAccess.WriteErrLog( "QueryList", ex.Message );
					throw new GDCException(ErrorCode.QueryService_Exception, "QueryService Exception", ex);
				}
				finally
				{
					session.Close();
				}
			}
		}
		#endregion

		#region Advanced Search
		/// <summary>
		/// 
		/// </summary>
		/// <param name="dicType"></param>
		/// <returns></returns>
		[Transaction(TransactionMode.Requires)]
		public int AddAdvancedSearch(BEGDC.CC.Entity.AdvancedSearch item)
		{
			IList list = null;
			int operationResult = 1;
	
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					list = session.Find ("FROM AdvancedSearch where employeeno='"+item.Employee.Id+"' and searchname ='"+item.SearchName+"'");
					if(list != null && list.Count > 0)
						operationResult = -1;
					else
					{

						session.Save(item);
						session.Flush();
					}
				}
				catch( Exception exp )
				{
					CCS.Utility.LogAccess.WriteErrLog( "AddAdvancedSearch", exp.Message );
					operationResult = 0;
					throw exp;
				}
				finally
				{
					session.Close();
				}
			}

			return operationResult;
		}
		public int SaveAdvancedSearch(BEGDC.CC.Entity.AdvancedSearch item)
		{
			IList list = null;
			int operationResult = 1;
	
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					list = session.Find ("FROM AdvancedSearch where employeeno='"+item.Employee.Id+"' and searchname ='"+item.SearchName+"' and SearchID != " + item.Id.ToString());
					if(list != null && list.Count > 0)
						operationResult = -1;

					BEGDC.CC.Entity.AdvancedSearch exist = this.GetAdvancedSearch(item.Id);
					if(exist != null)
					{
						exist.SearchName = item.SearchName;
						exist.Description = item.Description;
						exist.SearchSQL = item.SearchSQL;
						exist.SearchCondition = item.SearchCondition;
						exist.SearchField = item.SearchField;

						session.Update(exist);
						session.Flush();
					}
					else
						operationResult = 0;
				}
				catch( Exception exp )
				{
					CCS.Utility.LogAccess.WriteErrLog( "SaveAdvancedSearch", exp.Message );
					operationResult = 0;
					throw exp;
				}
				finally
				{
					session.Close();
				}
			}

			return operationResult;
		}

		[Transaction(TransactionMode.Requires)]
		public void DeleteSearchById( int searchId )
		{
			using( ISession session = _sessionManager.OpenSession() )
			{
				try
				{
					if( searchId > 0 )
					{
						session.Delete("FROM AdvancedSearch WHERE SearchID = " + searchId);
						session.Flush();
					}
				}
				catch(Exception ex)
				{
					CCS.Utility.LogAccess.WriteErrLog( "DeleteSearchById", ex.Message );
					throw ex;
				}
				finally
				{
					session.Close();
				}
			}
		}
		#endregion

		#endregion
	}
}
